﻿using ServiceCore.DbContext;
using ServiceCore.Entity.Core;
using ServiceCore.Entity.Model;
using ServiceCore.Entity.Param.Tag;

namespace ServiceCore.Service
{
    public partial class TagTypeService : SqlsugarServices<TagTypeInfo>
    {
        public BaseParam<TagTypeInfo, long> TagType_Save_Common_Verify(BaseParam<TagTypeInfo, long> arg)
        {
            if (arg == null)
                return new BaseParam<TagTypeInfo, long>("参数不能为空");
            if (arg.Param == null)
                return new BaseParam<TagTypeInfo, long>("参数不能为空");
            if (string.IsNullOrEmpty(arg.Param.Name))
                return new BaseParam<TagTypeInfo, long>("类型名称不能为空");
            //if (arg.Param.CreateUserId == 0)
            //    return new BaseParam<TagTypeInfo, long>("创建人不能为空");

            return arg;
        }

        public BaseParam<TagTypeInfo, long> TagType_Add_Verify(BaseParam<TagTypeInfo, long> arg)
        {
            arg = TagType_Save_Common_Verify(arg);
            if (string.IsNullOrEmpty(arg.ErrorMsg) == false)
                return arg;

            var argUser = new BaseParam<TagTypeInfo, TagTypeInfo>();
            argUser.Param = arg.Param;
            TagType_Exists(argUser, true);
            if (argUser.Data != null)
                return new BaseParam<TagTypeInfo, long>("类型名称已存在");

            arg.Param.CreateTime = DateTime.Now;
            return arg;
        }

        public BaseParam<TagTypeInfo, long> TagType_Edit_Verify(BaseParam<TagTypeInfo, long> arg)
        {
            arg = TagType_Save_Common_Verify(arg);
            if (string.IsNullOrEmpty(arg.ErrorMsg) == false)
                return arg;
            if (arg.Param.Id == 0)
                return new BaseParam<TagTypeInfo, long>("id不能为空");

            var argUser = new BaseParam<TagTypeInfo, TagTypeInfo>();
            argUser.Param = arg.Param;
            TagType_Exists(argUser, false);
            if (argUser.Data != null)
                return new BaseParam<TagTypeInfo, long>("类型名称已存在");

            return arg;
        }

        public BaseParam<TagDeleteParam, long> TagType_Del_Verify(BaseParam<TagDeleteParam, long> arg)
        {
            if (arg == null)
                return new BaseParam<TagDeleteParam, long>("参数不能为空");
            if (arg.Id == 0)
                return new BaseParam<TagDeleteParam, long>("id不能为空");
            if (arg.Param == null)
                arg.Param = new TagDeleteParam();

            arg.Param.TagIds = this.Context.Queryable<TagInfo>().Where(x => x.TagTypeId == arg.Id).Select(x => x.Id).ToList();

            return arg;
        }
    }
}
